<template>
  <div class="app-container">
    <!--      table-->
    <div class="view-title">订单管理</div>

    <div style="display: flex">
      <el-form
        ref="searchForm"
        :model="formSearch"
        label-width="80px"
        style="display: flex"
      >
        <el-form-item label="收货电话" label-width="100px">
          <el-input v-model="formSearch.address_phone"></el-input>
        </el-form-item>
        <el-form-item label="用户id" label-width="100px">
          <el-input v-model="formSearch.user_id"></el-input>
        </el-form-item>
      </el-form>
      <div class="search_box">
        <el-button type="primary" @click="handleSearch">搜索</el-button>
      </div>
    </div>

    <div class="add_box">
      <el-button type="danger" @click="handleExportTemplate"
        >导出模板</el-button
      >
      <el-button type="warning" @click="handleExpress">导入物流</el-button>
    </div>

    <el-table
      v-loading="loading"
      element-loading-text="正在加载中"
      :data="orderTableData"
      :header-cell-style="{
        borderColor: '#F0F0F0',
        height: '51px',
        background: '#F0F0F0',
        color: '#333',
      }"
      :row-style="{ height: '51px' }"
      :cell-style="{ borderColor: '#F0F0F0' }"
      style="width: 100%; margin-top: 20px"
    >
      <el-table-column prop="package_name" label="套餐名称" align="center">
      </el-table-column>
      <el-table-column prop="main_img" label="套餐图片" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.main_img" alt="" style="width: 100px" />
        </template>
      </el-table-column>
      <el-table-column prop="title" label="套餐介绍" align="center">
      </el-table-column>
      <el-table-column prop="address_name" label="收货人" align="center">
      </el-table-column>
      <el-table-column prop="address_phone" label="收货电话" align="center">
      </el-table-column>
      <el-table-column prop="address" label="收货地址" align="center">
      </el-table-column>
      <el-table-column prop="express_name" label="物流名称">
        <template slot-scope="scope">
          {{ scope.row.express_name ? scope.row.express_name : "暂无" }}
        </template>
      </el-table-column>
      <el-table-column prop="express_no" label="物流单号">
        <template slot-scope="scope">
          {{ scope.row.express_no ? scope.row.express_no : "暂无" }}
        </template>
      </el-table-column>
      <el-table-column prop="order_id" label="订单id" align="center">
      </el-table-column>
      <el-table-column prop="is_pay" label="订单状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.is_pay == 1"
            ><el-tag type="info">待付款</el-tag></span
          >
          <span v-else-if="scope.row.is_pay == 2"><el-tag>待发货</el-tag></span>
          <span v-else-if="scope.row.is_pay == 3"
            ><el-tag type="success">待收货</el-tag></span
          >
          <span v-else-if="scope.row.is_pay == 4"
            ><el-tag type="warning">退款/售后</el-tag></span
          >
          <span v-else-if="scope.row.is_pay == 5"
            ><el-tag type="danger">已取消</el-tag></span
          >
        </template>
      </el-table-column>
      <el-table-column prop="money" label="订单价格" align="center">
      </el-table-column>
      <el-table-column prop="user_id" label="用户id" align="center">
      </el-table-column>
      <el-table-column prop="created_at" label="创建时间" align="center">
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button
            @click="handleCheck(scope.row)"
            type="text"
            size="small"
            v-permission="['order:view']"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="page_box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNow"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <!--      查看订单-->
    <el-dialog title="查看订单" :visible.sync="checkDialogVisible" width="70%">
      <div>
        <el-table :data="checkTableData" style="width: 100%">
          <el-table-column prop="package_name" label="套餐名称">
          </el-table-column>
          <el-table-column prop="main_img" label="套餐图片">
            <template slot-scope="scope">
              <img
                :src="scope.row.main_img"
                alt=""
                style="width: 100%; height: 100%"
              />
            </template>
          </el-table-column>
          <el-table-column prop="title" label="套餐介绍"> </el-table-column>
          <el-table-column prop="address_name" label="收货人">
          </el-table-column>
          <el-table-column prop="address_phone" label="收货电话">
          </el-table-column>
          <el-table-column prop="address" label="收货地址"> </el-table-column>

          <el-table-column prop="express_name" label="物流名称">
            <template slot-scope="scope">
              {{ scope.row.express_name ? scope.row.express_name : "暂无" }}
            </template>
          </el-table-column>
          <el-table-column prop="express_no" label="物流单号">
            <template slot-scope="scope">
              {{ scope.row.express_no ? scope.row.express_no : "暂无" }}
            </template>
          </el-table-column>
          <el-table-column prop="order_id" label="订单id"> </el-table-column>
          <el-table-column prop="is_pay" label="订单状态">
            <template slot-scope="scope">
              <span v-if="scope.row.is_pay == 1">待付款</span>
              <span v-else-if="scope.row.is_pay == 2">待发货</span>
              <span v-else-if="scope.row.is_pay == 3">待收货</span>
              <span v-else-if="scope.row.is_pay == 4">退款/售后</span>
              <span v-else-if="scope.row.is_pay == 5">已取消</span>
            </template>
          </el-table-column>
          <el-table-column prop="money" label="订单价格"> </el-table-column>
          <el-table-column prop="user_id" label="用户id"> </el-table-column>
          <el-table-column prop="created_at" label="创建时间">
          </el-table-column>
        </el-table>
      </div>
    </el-dialog>

    <!-- 导入物流弹窗 -->
    <import-express ref="refImportExpress" @successBatch="successBatch" />
  </div>
</template>

<script>
import {
  getAppOrderList,
  getAppOrderInfo,
  addAppOrder,
  updateAppOrder,
  deleteAppOrder,
} from "@/api/order.js";
import importExpress from "./components/importExpress.vue";

export default {
  name: "order",
  components: {
    importExpress,
  },
  data() {
    return {
      formSearch: {
        address_phone: "",
        user_id: "",
      },
      orderTableData: [],
      checkId: "",
      checkTableData: [],
      checkDialogVisible: false,
      pageNow: 1,
      pageSize: 10,
      total: null,
      loading: true,
    };
  },
  mounted() {
    this.getAppOrderList();
  },
  methods: {
    // 获取订单列表
    getAppOrderList() {
      getAppOrderList({
        address_phone: this.formSearch.address_phone,
        user_id: this.formSearch.user_id,
        page_now: this.pageNow,
        page_size: this.pageSize,
      }).then((res) => {
        if (res.code == 1) {
          this.loading = false;
          this.orderTableData = res.data;
          this.total = res.page_info.total_count;
        } else {
          this.loading = false;
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 查看订单
    getAppOrderInfo() {
      getAppOrderInfo({
        id: this.checkId,
      }).then((res) => {
        if (res.code == 1) {
          const arr = [];
          arr.push(res.data);
          this.checkTableData = arr;
          this.checkDialogVisible = true;
        } else {
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 搜索
    handleSearch() {
      this.getAppOrderList();
    },

    // 查看
    handleCheck(val) {
      this.checkId = val.id;
      this.getAppOrderInfo();
    },

    // 分页
    handleSizeChange(val) {
      this.pageSize = val;
      this.getAppOrderList();
    },
    handleCurrentChange(val) {
      this.pageNow = val;
      this.pageSize = 10;
      this.getAppOrderList();
    },

    // 导入物流
    handleExpress() {
      this.$refs.refImportExpress.dialogVisibleImport = true;
    },

    // 导出模板
    handleExportTemplate() {
      let url = "https://app.server.jkcae.com/csv/物流模板.csv";
      location.href = url;
    },

    // 导入物流成功
    successBatch(val) {
      this.$refs.refImportExpress.dialogVisibleImport = false;
      this.getAppOrderList();
    },
  },
};
</script>

<style scoped lang="scss">
.search_box {
  margin-left: 10px;
}
</style>
